﻿Public Class clsInhTrZlecenie
    Inherits clsTrZlecenie

#Region "Attributes"

    Private mvarPozycjeZlecenia As Collection(Of clsInhTrPozycjaZlecenia)
    Private mvarPozycjeZleceniaDel As Collection(Of clsInhTrPozycjaZlecenia)

    Private mvarKlient As clsKlient
    Private mvarStatus As clsStatus
    Private mvarNumerZlecenia As Integer

    Private mvarTermin As Date
    Private mvarStatusId As Integer


#End Region

#Region "Properties"

    Public Property PozycjeZlecenia() As Collection(Of clsInhTrPozycjaZlecenia)
        Get
            Return mvarPozycjeZlecenia
        End Get
        Set(ByVal value As Collection(Of clsInhTrPozycjaZlecenia))
            mvarPozycjeZlecenia = value
        End Set
    End Property

    Public Property PozycjeZleceniaDel() As Collection(Of clsInhTrPozycjaZlecenia)
        Get
            Return mvarPozycjeZleceniaDel
        End Get
        Set(ByVal value As Collection(Of clsInhTrPozycjaZlecenia))
            mvarPozycjeZleceniaDel = value
        End Set
    End Property

    Public Property Klient As clsKlient
        Get
            Return mvarKlient
        End Get
        Set(ByVal value As clsKlient)
            mvarKlient = value
        End Set
    End Property

    Public Property Status As clsStatus
        Get
            Return mvarStatus
        End Get
        Set(ByVal value As clsStatus)
            mvarStatus = value
        End Set
    End Property



    Public Property InhTermin As String
        Get
            Return CStr(mvarTermin)
        End Get
        Set(ByVal value As String)
            mvarTermin = CDate(value)
        End Set
    End Property

    Public Property InhStatusId As Integer
        Get
            Return mvarStatusId
        End Get
        Set(ByVal value As Integer)
            mvarStatusId = value
        End Set
    End Property

    Public Property NumerZlecenia As Integer
        Get
            Return mvarNumerZlecenia
        End Get
        Set(ByVal value As Integer)
            mvarNumerZlecenia = value
        End Set
    End Property
#End Region

#Region "Methods"

    Public Sub New()
        MyBase.New()

        mvarPozycjeZlecenia = New Collection(Of clsInhTrPozycjaZlecenia)
        mvarPozycjeZleceniaDel = New Collection(Of clsInhTrPozycjaZlecenia)
    End Sub

    Public Sub New(ByVal ID As Integer, ByVal lvarDbHolder As clsDataBase, Wyzn As Boolean)

        MyBase.New(ID, lvarDbHolder, VAR_ERROR_HANDLER)
        mvarPozycjeZlecenia = New Collection(Of clsInhTrPozycjaZlecenia)
        mvarPozycjeZleceniaDel = New Collection(Of clsInhTrPozycjaZlecenia)

        mvarKlient = New clsKlient(MyBase.KlientID, lvarDbHolder, VAR_ERROR_HANDLER)
        mvarStatus = New clsStatus(MyBase.StatusId, lvarDbHolder, VAR_ERROR_HANDLER)
        Dim lvarTabel As DataTable = PobierzWszystkiePozycje(lvarDbHolder, ID)

        If Not lvarTabel Is Nothing Then
            For Each lvarRow As DataRow In lvarTabel.Rows
                mvarPozycjeZlecenia.Add(New clsInhTrPozycjaZlecenia(CInt(lvarRow.Item("id")), lvarDbHolder, Wyzn))
            Next
        End If

    End Sub

    Public Overloads Sub Save(ByVal lvarDbHolder As clsDataBase)

        ' lvarDbHolder.BeginTrans()

        MyBase.Save(lvarDbHolder, VAR_ERROR_HANDLER)

        For Each lvarPozycja As clsInhTrPozycjaZlecenia In mvarPozycjeZleceniaDel
            lvarPozycja.Delete(lvarDbHolder)
        Next
        For Each lvarPozycja As clsInhTrPozycjaZlecenia In mvarPozycjeZlecenia
            lvarPozycja.ZlecenieId = MyBase.ID
            lvarPozycja.Save(lvarDbHolder)
        Next

        '   lvarDbHolder.CommitTrans()

    End Sub


    Shared Function PobierzWszystkiePozycje(ByVal lvarDbHolder As clsDataBase, IdZlecenia As Integer) As DataTable

        Dim lvarReader As OdbcDataReader = Nothing
        Dim lvarReturn As DataTable

        If lvarDbHolder.IsOpen Then
            Dim lvarSqlRunner As New clsSqlRunner(lvarDbHolder, "queries_application_tracking", "cls_zlecenie", 1, VAR_ERROR_HANDLER)
            '       SELECT id FROM oem.pozycje_zlecenia WHERE zlecenie_id=<%p_id_zlecenia%> ORDER BY id;

            With lvarSqlRunner
                .AddParameter("p_id_zlecenia", CStr(IdZlecenia))
                lvarReader = .ExecuteQuery
            End With

            If lvarReader.HasRows Then
                lvarReturn = New DataTable
                lvarReturn.Load(lvarReader)
            End If

        End If

        Return lvarReturn

    End Function

    Public Sub InformacjeZlecenia(ByVal lvarDbHolder As clsDataBase, Id As Integer)

        Dim lvarReader As OdbcDataReader = Nothing

        If lvarDbHolder.IsOpen Then
            Dim lvarSqlRunner As New clsSqlRunner(lvarDbHolder, "queries_application_tracking", "cls_zlecenie", 2, VAR_ERROR_HANDLER)

            With lvarSqlRunner
                .AddParameter("p_id", CStr(Id))
                lvarReader = .ExecuteQuery
            End With

            If lvarReader.HasRows Then
                mvarKlient = New clsKlient(CInt(IIf(IsDBNull(lvarReader.Item("id")), -1, lvarReader.Item("id"))), lvarDbHolder, VAR_ERROR_HANDLER)
                mvarTermin = CDate(IIf(IsDBNull(lvarReader.Item("termin")), -1, lvarReader.Item("termin")))
                mvarStatusId = CInt(IIf(IsDBNull(lvarReader.Item("status_id")), -1, lvarReader.Item("status_id")))
                mvarNumerZlecenia = CInt(lvarReader.Item("numer"))
            End If
        End If

    End Sub

    Shared Function ZmienStatus(ByVal lvarDbHolder As clsDataBase, IdZlecenia As Integer, IdStatus As Integer) As Boolean

        Dim lvarReader As OdbcDataReader = Nothing
        Dim lvarReturn As Boolean

        If lvarDbHolder.IsOpen Then
            Dim lvarSqlRunner As New clsSqlRunner(lvarDbHolder, "queries_application_tracking", "cls_zlecenie", 3, VAR_ERROR_HANDLER)
            '      

            With lvarSqlRunner
                .AddParameter("p_zlecenia_id", CStr(IdZlecenia))
                .AddParameter("p_status_id", CStr(IdStatus))
                lvarReader = .ExecuteQuery
            End With

        End If

        Return lvarReturn

    End Function

    Shared Function PobierzIdZleceniaTr(ByVal lvarDbHolder As clsDataBase, IdZlecenia As Integer) As Integer

        Dim lvarReader As OdbcDataReader = Nothing
        Dim lvarReturn As Integer

        If lvarDbHolder.IsOpen Then
            Dim lvarSqlRunner As New clsSqlRunner(lvarDbHolder, "queries_application_tracking", "cls_zlecenie", 4, VAR_ERROR_HANDLER)
            '       SELECT id FROM tracking.zlecenie WHERE oem_id=<%p_id_zlecenia%>;

            With lvarSqlRunner
                .AddParameter("p_oem_id", CStr(IdZlecenia))
                lvarReader = .ExecuteQuery
            End With

            If lvarReader.HasRows Then
                lvarReturn = CInt(IIf(IsDBNull(lvarReader.Item("id")), -1, lvarReader.Item("id")))
            End If

        End If

        Return lvarReturn

    End Function

#End Region

End Class
